Passed
Push — develop ( 3b1736...62d6da )
by Paul
03:12
created

pollux.metabox.init   A

Complexity

Conditions 1
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 11
rs 9.4285

1 Function

Rating   Name   Duplication   Size   Complexity  
A 0 7 2
1
/** global: wp, CodeMirror */
2
3
var pollux = {
4
	editors: {},
5
	media: {
6
		featured: {},
7
	},
8
	metabox: {},
9
	tabs: {},
10
};
11
12
/**
13
 * @return bool
14
 */
15
pollux.classListAction = function( bool )
16
{
17
	return bool ? 'add' : 'remove';
18
};
19
20
/**
21
 * @return void
22
 */
23
pollux.media.featured.init = function()
24
{
25
	jQuery( '#postimagediv' )
26
	.on( 'click', '#pollux-set-featured', function( ev ) {
27
		ev.preventDefault();
28
		wp.media.view.settings.post.featuredImageId = Math.round( jQuery( '#featured' ).val() );
29
		pollux.media.featured.frame = wp.media.featuredImage.frame;
30
		pollux.media.featured.frame().open();
31
	})
32
	.on( 'click', '#pollux-remove-featured', function( ev ) {
33
		ev.preventDefault();
34
		pollux.media.featured.set(-1);
35
	});
36
};
37
38
/**
39
 * @return void
40
 */
41
pollux.media.featured.select = function()
42
{
43
	if( !wp.media.view.settings.post.featuredImageId )return;
44
	var selection = this.get( 'selection' ).single();
45
	pollux.media.featured.set( selection ? selection.id : -1 );
46
};
47
48
/**
49
 * @return void
50
 */
51
pollux.media.featured.set = function( id )
52
{
53
	wp.media.view.settings.post.featuredImageId = Math.round( id );
54
	wp.media.post( 'pollux/archives/featured/html', {
55
		_wpnonce: document.querySelector( '#_wpnonce' ).value,
56
		post_type: document.querySelector( '#archive-type' ).value,
57
		thumbnail_id: id,
58
	}).done( function( html ) {
59
		document.querySelector( '#postimagediv > .inside' ).innerHTML = html;
60
	});
61
};
62
63
/**
64
 * @return bool
65
 */
66
pollux.metabox.hasValue = function( el )
67
{
68
	if( el.type === 'checkbox' ) {
69
		return el.checked === true;
70
	}
71
	return el.value !== '';
72
};
73
74
/**
75
 * @return void
76
 */
77
pollux.metabox.init = function()
78
{
79
	var depends = document.querySelectorAll( '.rwmb-input [data-depends]' );
80
	[].forEach.call( depends, function( el ) {
81
		var dependency = pollux.metabox.setVisibility( el );
82
		var event = dependency.type === 'checkbox' ? 'change' : 'keyup';
83
		dependency.addEventListener( event, function() {
84
			pollux.metabox.setVisibility( el );
85
		});
86
	});
87
};
88
89
/**
90
 * @return element
91
 */
92
pollux.metabox.setVisibility = function( el )
93
{
94
	var dependency = document.getElementById( el.getAttribute( 'data-depends' ));
95
	var action = pollux.classListAction( !pollux.metabox.hasValue( dependency ));
96
	el.closest( '.rwmb-field' ).classList[action]( 'hidden' );
97
	return dependency;
98
};
99
100
/**
101
 * @return void
102
 */
103
pollux.tabs.init = function()
104
{
105
	pollux.tabs.active = document.querySelector( '#pollux-active-tab' );
106
	pollux.tabs.referrer = document.querySelector( 'input[name="_wp_http_referer"]' );
107
	pollux.tabs.tabs = document.querySelectorAll( '.pollux-tabs a' );
108
	pollux.tabs.views = document.querySelectorAll( '.pollux-config .form-table' );
109
110
	[].forEach.call( pollux.tabs.tabs, function( tab, index ) {
111
		var active = location.hash ? tab.getAttribute( 'href' ).slice(1) === location.hash.slice(2) : index === 0;
112
		if( active ) {
113
			pollux.tabs.setTab( tab );
114
		}
115
		tab.addEventListener( 'click', pollux.tabs.onClick );
116
		tab.addEventListener( 'touchend', pollux.tabs.onClick );
117
	});
118
};
119
120
/**
121
 * @return void
122
 */
123
pollux.tabs.onClick = function( ev )
124
{
125
	ev.preventDefault();
126
	this.blur();
127
	pollux.tabs.setTab( this );
128
	location.hash = '!' + this.getAttribute( 'href' ).slice(1);
129
};
130
131
/**
132
 * @return void
133
 */
134
pollux.tabs.setReferrer = function( index )
135
{
136
	var referrerUrl = pollux.tabs.referrer.value.split('#')[0] + '#!' + pollux.tabs.views[index].id;
137
	pollux.tabs.referrer.value = referrerUrl;
138
};
139
140
/**
141
 * @return void
142
 */
143
pollux.tabs.setTab = function( el )
144
{
145
	[].forEach.call( pollux.tabs.tabs, function( tab, index ) {
146
		var action = pollux.classListAction( tab === el );
147
		if( action === 'add' ) {
148
			pollux.tabs.active.value = pollux.tabs.views[index].id;
149
			pollux.tabs.setReferrer( index );
150
			pollux.tabs.setView( index );
151
		}
152
		tab.classList[action]( 'nav-tab-active' );
153
	});
154
};
155
156
/**
157
 * @return void
158
 */
159
pollux.tabs.setView = function( idx )
160
{
161
	[].forEach.call( pollux.tabs.views, function( view, index ) {
162
		var action = pollux.classListAction( index !== idx );
163
		view.classList[action]( 'ui-tabs-hide' );
164
	});
165
};
166
167
/**
168
 * @return void
169
 */
170
pollux.editors.init = function()
171
{
172
	pollux.editors.all = [];
173
	[].forEach.call( document.querySelectorAll( '.pollux-code' ), function( editor, index ) {
174
		pollux.editors.all[index] = CodeMirror.fromTextArea( editor, {
175
			gutters: ['CodeMirror-lint-markers'],
176
			highlightSelectionMatches: { wordsOnly: true },
177
			lineNumbers: true,
178
			lint: true,
179
			mode: 'text/yaml',
180
			showInvisibles: true,
181
			showTrailingSpace: true,
182
			styleActiveLine: true,
183
			tabSize: 2,
184
			theme: 'pollux',
185
			viewportMargin: Infinity,
186
		});
187
		pollux.editors.all[index].setOption( 'extraKeys', {
188
			Tab: function( cm ) {
189
				var spaces = Array( cm.getOption( 'indentUnit' ) + 1 ).join( ' ' );
190
				cm.replaceSelection( spaces );
191
			},
192
		});
193
		pollux.editors.all[index].display.wrapper.setAttribute( 'data-disabled', editor.getAttribute( 'data-disabled' ));
194
		if( editor.readOnly ) {
195
			pollux.editors.all[index].setOption( 'theme', 'disabled' );
196
			pollux.editors.all[index].setOption( 'readOnly', 'nocursor' );
197
		}
198
	});
199
};
200
201
jQuery(function() {
202
	pollux.editors.init();
203
	pollux.media.featured.init();
204
	pollux.metabox.init();
205
	pollux.tabs.init();
206
});
207